'''
Created on 13.12.2012

@author: fre
'''
from game import GameLogic
from game.player.alpha_beta_player.alpha_beta_framework.moveordering.MoveComparator import MoveComparator
from game.datastructure.PlayerColor import PlayerColor

class MostNeighboursMoveOrdering(MoveComparator):
    
    def evaluate(self, action):
        x,y = action
        hLen = len(self.field.history)
        self.field.place(self.color, x,y)
        GameLogic.applyAction(self.field, action, self.color)
        maxLen = GameLogic.getMaxLenFromPoint(self.field, action, 1, GameLogic.WIN_LENGTH)
        if maxLen is None:
            maxLen = 0
        else:
            maxLen = maxLen[0]
        self.field.undo(len(self.field.history)-hLen)
        neighboursCount = self.countNeighboursInColor(action)
        return neighboursCount + maxLen
            
    def countNeighboursInColor(self, action):
        neighbours = GameLogic.getNeighbours(self.field, action)
        count = 0
        for neighbour in neighbours:
            x,y = neighbour
            if self.field.get(x,y) != PlayerColor.NONE:
                count -= 1
        return count
        